package com.af.security.auth.util.handler;

import com.af.security.auth.util.AfAuthenticationUtils;
import com.af.service.AfRedirectService;
import com.af.system.api.result.AfResultUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : zhenyun.su
 * @since : 2019/6/24
 * @Common : 登出成功触发执行
 * 1. 清空手机用户信息和菜单数据
 * 2. 返回登出成功json数据
 *
 */

@Component
public class AfLogoutSuccessHandler implements LogoutSuccessHandler {
    private AfRedirectService redirectService;

    public AfLogoutSuccessHandler(AfRedirectService redirectService) {
        this.redirectService = redirectService;
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        AfAuthenticationUtils.removeUserDetails(httpServletRequest);
        AfAuthenticationUtils.removePrincipal(httpServletRequest);
        redirectService.redirectJson(httpServletResponse, AfResultUtils.success());
    }
}
